//
//  VisitorView.swift
//  Sina
//
//  Created by duhui on 15/9/28.
//  Copyright © 2015年 沈方武. All rights reserved.
//

import UIKit
import SnapKit

@objc
protocol VisitorViewDelegat : NSObjectProtocol
{
    optional func visitorViewDidRegistedButtonClick(view : VisitorView)
    optional func visitorViewDidloginButtonClick(view : VisitorView)

}

class VisitorView: UIView {
 
    weak var delegate : VisitorViewDelegat?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(red: 232.0/255.0, green: 233.0/255.0, blue: 232.0/255.0, alpha: 1.0)
        // 添加界面元素
        setUpUI()
  
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUpVisitorView(isHome : Bool , homeIconImageNmae : String , info : String)
    {
        icon.hidden = !isHome
        homeIcon.image = UIImage(named: homeIconImageNmae)
        message.text = info
        
        if isHome{
            
            startAnimation()
        }
        
    }
    
    
    // 添加界面元素
    private func setUpUI()
    {
        addSubview(icon)
        addSubview(homeIcon)
        addSubview(message)
        addSubview(maskBgView)
        addSubview(loginButton)
        addSubview(registerButton)
        
 /// 布局
        icon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        homeIcon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        message.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(icon.snp_bottom)
            make.centerX.equalTo(self)
            make.width.equalTo(300)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(message)
            make.top.equalTo(message.snp_bottom).offset(20)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(message)
            make.top.equalTo(message.snp_bottom).offset(20)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        
        maskBgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(0)
        }

    }
    
    
    private func startAnimation()
    {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
        icon.layer.addAnimation(anim, forKey: nil)
    }
    
    

     // 懒加载
     /// 转盘
    private lazy var icon : UIImageView = {
     
        return UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    }()
    
    /// home图标
   private lazy var homeIcon : UIImageView = {
       return  UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    }()
    /// 文本
    private lazy var message : UILabel = {
         let lab = UILabel()
        lab.text = "哈刷卡啊觉得很舒服撒烦恼开始发你看不想尝试说的就是看说说"
        lab.numberOfLines = 0
        lab.textColor = UIColor.grayColor()
        return lab
    }()
    /// 登陆按钮
    private lazy var loginButton : UIButton = {
        let button = UIButton()
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        // 添加监听
        button.addTarget(self, action: Selector("loginClick"), forControlEvents: UIControlEvents.TouchUpInside)
        return button
        
    }()
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 注册按钮点击监听
        btn.addTarget(self, action: Selector("registerClick"), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    /// 背景图片
    private lazy var maskBgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
        }()

    // 监听方法
    func registerClick()
    {
        delegate?.visitorViewDidRegistedButtonClick!(self)
    }
    
    @objc private func loginClick()
    {
        
        delegate?.visitorViewDidloginButtonClick!(self)
    }
    
    
    
}
